package com.shaoshaohuo.app.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.utils.im.IMFactory;
import com.shaoshaohuo.app.framework.AccountManager;
import com.shaoshaohuo.app.framework.ActivityCollector;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.net.HttpConfig.WebPage;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.umeng.analytics.MobclickAgent;

/**
 * 设置
 * 
 * @author cao
 *
 */
public class SettingActivity extends BaseActivity implements OnClickListener {

	private TopbarView mTopbarView;

	private View mFeedbackLayout;
	private View mXinshouLayout;
	private View mAboutLayout;
	private Button mLogoutButton;



	private TextView tv_version_name;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);

		initView();
		setUpView();
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		mFeedbackLayout = this.findViewById(R.id.layout_feedback);
		mXinshouLayout = this.findViewById(R.id.layout_xinshouzhinan);
		mAboutLayout = this.findViewById(R.id.layout_about_us);
		mLogoutButton = (Button) this.findViewById(R.id.button_logout);

		tv_version_name = (TextView) findViewById(R.id.tv_version_name);
	}

	private void setUpView() {
		mTopbarView.setCenterText("设置");
		mTopbarView.setLeftView(true, true);

		mFeedbackLayout.setOnClickListener(this);
		mXinshouLayout.setOnClickListener(this);
		mAboutLayout.setOnClickListener(this);
		mLogoutButton.setOnClickListener(this);

		if (!AccountManager.isLogin()) {
			mLogoutButton.setVisibility(View.GONE);
		}
		
		tv_version_name.setText("当前版本："+DeviceHelper.getVersionName(this));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_feedback:
			Intent feedbackIntent = new Intent(this, FeedbackActivity.class);
			startActivity(feedbackIntent);
			break;
		case R.id.layout_xinshouzhinan:
			Intent intent = new Intent(this, WebBrowserActivity.class);
			intent.putExtra(WebBrowserActivity.TITLE, "新手指导");
			intent.putExtra(WebBrowserActivity.URL, WebPage.GUIDE);
			startActivity(intent);
			break;
		case R.id.layout_about_us:
			Intent aboutIntent = new Intent(this, WebBrowserActivity.class);
			aboutIntent.putExtra(WebBrowserActivity.TITLE, "关于我们");
			aboutIntent.putExtra(WebBrowserActivity.URL, WebPage.ABOUT);
			startActivity(aboutIntent);
			break;
		case R.id.button_logout:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("确定退出登录？");
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					AccountManager.logout(SettingActivity.this);
					SharedPreferencesHelper.setBoolean(getApplicationContext(),
							SharedPreferencesHelper.Field.IS_COME_BACK, false);
					ActivityCollector.finishAll();
					Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
					intent.putExtra(LoginActivity.TAG_IS_CAN_NOT_BACK, true);
					startActivity(intent);
					IMFactory.logout();
                    MobclickAgent.onProfileSignOff();
				}
			});
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			});
			builder.create().show();
			break;
		}
	}


}
